﻿#include "SerialPortController.h"

#include <QDebug>

namespace Core {

	SerialPortController::SerialPortController(QObject *parent)
		: QObject(parent)
		, m_pAsyncSerialPort(nullptr)
		, m_iSendMsgType(0)
		, m_iRecvMsgType(0)
		, m_iSendMsgTotalLength(0)
		, m_iRecvMsgTotalLength(0)
	{
		m_pAsyncSerialPort = new Common::AsyncSerialPort(this);
		connect(m_pAsyncSerialPort, &Common::AsyncSerialPort::signalRecvSerialMsg, this, &SerialPortController::slotRecvSerialMsg);
	}

	SerialPortController::~SerialPortController()
	{
		if (m_pAsyncSerialPort != nullptr)
		{
			delete m_pAsyncSerialPort;
			m_pAsyncSerialPort = nullptr;
		}
	}

	QStringList SerialPortController::getSerialPortList()
	{
		return Common::SerialPortConfig::getAvailableSerialPort();
	}

	bool SerialPortController::setOpenSerialPort(QString strPortName, int iBaudRateIndex, int iDataBitsIndex, int iParityIndex, int iStopBitsIndex, int iFlowControlIndex)
	{
		QSerialPort::BaudRate baudRate; 
		if (iBaudRateIndex == 0)
		{
			baudRate = QSerialPort::Baud9600;
		}
		else if(iBaudRateIndex == 1)
		{
			baudRate = QSerialPort::Baud19200;
		}
		else if (iBaudRateIndex == 2)
		{
			baudRate = QSerialPort::Baud38400;
		}
		else if (iBaudRateIndex == 3)
		{
			baudRate = QSerialPort::Baud57600;
		}
		else if (iBaudRateIndex == 4)
		{
			baudRate = QSerialPort::Baud115200;
		}

		QSerialPort::DataBits dataBits;
		if (iDataBitsIndex == 0) 
		{
			dataBits = QSerialPort::Data5;
		}
		else if (iDataBitsIndex == 1)
		{
			dataBits = QSerialPort::Data6;
		}
		else if (iDataBitsIndex == 2)
		{
			dataBits = QSerialPort::Data7;
		}
		else if (iDataBitsIndex == 3)
		{
			dataBits = QSerialPort::Data8;
		}

		QSerialPort::Parity parity;
		if (iParityIndex == 0)
		{
			parity = QSerialPort::NoParity;
		}
		else if (iParityIndex == 1)
		{
			parity = QSerialPort::EvenParity;
		}
		else if (iParityIndex == 2)
		{
			parity = QSerialPort::OddParity;
		}
		else if (iParityIndex == 3)
		{
			parity = QSerialPort::MarkParity;
		}
		else if (iParityIndex == 4)
		{
			parity = QSerialPort::SpaceParity;
		}

		QSerialPort::StopBits stopBits;
		if (iStopBitsIndex == 0)
		{
			stopBits = QSerialPort::OneStop;
		}
		else if (iStopBitsIndex == 1)
		{
			stopBits = QSerialPort::OneAndHalfStop;
		}
		else if (iStopBitsIndex == 2)
		{
			stopBits = QSerialPort::TwoStop;
		}

		QSerialPort::FlowControl flowControl;
		if (iFlowControlIndex == 0)
		{
			flowControl = QSerialPort::NoFlowControl;
		}
		else if (iFlowControlIndex == 1)
		{
			flowControl = QSerialPort::HardwareControl;
		}
		else if (iFlowControlIndex == 2)
		{
			flowControl = QSerialPort::SoftwareControl;
		}

		return m_pAsyncSerialPort->open(strPortName, baudRate, dataBits, parity, stopBits, flowControl);
	}

	void SerialPortController::setCloseSerialPort()
	{
		m_pAsyncSerialPort->close();
	}

	bool SerialPortController::getSerialPortIsOpen()
	{
		return m_pAsyncSerialPort->isOpen();
	}

	void SerialPortController::setSendSerialMsg(const QString strSendMsg)
	{
		if(strSendMsg.isEmpty())
		{
			return;
		}

		emit signalSerialPortMsgInfo(0, strSendMsg);

		QByteArray sendByte;
		if (m_iSendMsgType == 0)
		{
			sendByte = strSendMsg.toLatin1();
		}
		else
		{
			sendByte = hexStringToByteArray(strSendMsg);
		}
		m_pAsyncSerialPort->sendSerialMsg(sendByte);

		m_iSendMsgTotalLength = m_iSendMsgTotalLength + sendByte.size();
		MessDispath::instance()->setSerialPortSendMsgLength(m_iSendMsgTotalLength);
	}

	void SerialPortController::setSendMsgType(int iType)
	{
		m_iSendMsgType = iType;
	}

	void SerialPortController::setRecvMsgType(int iType)
	{
		m_iRecvMsgType = iType;
	}

	void SerialPortController::setClearMsgLength()
	{
		m_iSendMsgTotalLength = 0;
		m_iRecvMsgTotalLength = 0;

		MessDispath::instance()->setSerialPortSendMsgLength(0);
		MessDispath::instance()->setSerialPortRecvMsgLength(0);
	}

	void SerialPortController::slotRecvSerialMsg(QByteArray buff)
	{
		QString strMgs;
		if (m_iRecvMsgType == 0)
		{
			strMgs = QString(buff);
		}
		else if(m_iRecvMsgType == 1)
		{
			strMgs = ByteArrayToHexString(buff);
		}
		emit signalSerialPortMsgInfo(1, strMgs);

		m_iRecvMsgTotalLength = m_iRecvMsgTotalLength + buff.size();
		MessDispath::instance()->setSerialPortRecvMsgLength(m_iRecvMsgTotalLength);
	}

	QString SerialPortController::ByteArrayToHexString(QByteArray data)
	{
		QString ret(data.toHex().toUpper());
		int len = ret.length() / 2;
		for (int i = 1; i < len; i++)
		{
			ret.insert(2 * i + i - 1, " ");
		}
		return ret;
	}

	QByteArray SerialPortController::hexStringToByteArray(QString hexString)
	{
		// 移除字符串中可能存在的空格
		QString str = hexString.remove(' ');
		// 使用fromHex方法进行转换
		return QByteArray::fromHex(str.toUtf8());
	}

}//namespace Core